import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import twitter4j.Paging;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.auth.RequestToken;

public class IG extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 729494668148160937L;

	JPanel panelTweet = new JPanel();
	Client twitter = new Client();

	public IG() {
		
		String id = null;
		String pw = null;
		String pin = null;
		int rm = 0;
		Account ses = twitter.sessionTest();
		Scanner sc = new Scanner(System.in);

		// Remember me ?
		// If box checked - create file session.tw with credentials
		// Check if file exists - if so login directly with these credentials
		// If option deconnect used while quitting the client - delete
		// session.tw

		if (ses != null) {
			twitter.login(ses.getUserName(), 1);
		} else {
			System.out.println("ID :");
			id = sc.nextLine();
			System.out.println("PW :");
			pw = sc.nextLine();
			System.out.println("Remember me ? No : 0, Yes : 1");
			rm = sc.nextInt();
			int rep = twitter.testLogin(id, pw);

			// Create Account ? - If storeCredential.tw doesn't exist, create
			// it.
			// Ask for userName & Password
			// Link with pin
			// Store new account in storeCredentials.tw

			// Connection ?
			// UserName in db & matches password - Tokens akready in db : OK
			// UserName in db & wrong password : Try Again
			// UserName not in db : Please register

			// 0 : if username matches password
			// 1 : if username doesn't matches password
			// 2 : if username isn't in db
			// -1 : if storeCredentials.tw doesn't exist
			// -2 : if IO error

			if (rep == 0) {
				twitter.login(id, rm);
			} else if (rep == 1) {
				System.out.println("Bad password :(");
				// testLogin(new id, new pw);
			} else if (rep == 2) {
				Account newUser;
				try {
					newUser = twitter.createAccount(id, pw);
					RequestToken requestToken = twitter.requestPin();
					pin = sc.nextLine();
					twitter.loginWithPin(newUser, pin, requestToken, rm);
				} catch (UserAlreadyRegisteredException e) {
					// User Already Exists
				}
				

			} else if (rep == -1) {
				twitter.createCredentialsDB();
				Account newUser;
				try {
					newUser = twitter.createAccount(id, pw);
					RequestToken requestToken = twitter.requestPin();
					pin = sc.nextLine();
					twitter.loginWithPin(newUser, pin, requestToken, rm);
				} catch (UserAlreadyRegisteredException e) {
					// User Already Exists
				}
				
			} else {
				// oops sth went wrong - try again ?
			}

		}
		
		/*
		 * Getting Home Timeline
		 */

		Paging p = new Paging(1, 40);
		ResponseList<Status> homeTimeline = twitter.getHomeTimeline(p);
		int num = 0;
		for (Status status : homeTimeline) {
			try {
				System.out.println("("+num+")"+"@"+status.getUser().getName()+": " + status.getText());
			} catch (ArrayIndexOutOfBoundsException e) {

			} catch (NullPointerException e) {

			}
			num++;
		}
		
		/*
		 * Getting a Tree
		 */
		Scanner s = new Scanner(System.in);
		int choice = s.nextInt();
//		Node reply = twitter.getReply(homeTimeline.get(choice));
//		reply.saveTree();
//		System.out.println(reply.getText());
		Node load = new Node(homeTimeline.get(choice));
		load.load();
		System.out.println(load.getText());
		

//		/*
//		 * Sending a tweet
//		 */
//
//		System.out.println("Entrer un tweet :");
//		String text = sc.nextLine();
//		twitter.sendTweet(text);
		
//		/*
//		 * Loading stored tweets
//		 */
//		
//		for(Status stat : twitter.homeTimelineTweets.statusList) {
//		System.out.println(stat.getText());
//		}
		

		/*
		 * Deconnection ?
		 */
		int deconnexion = 0;
		System.out.println("Deconnexion ? No : 0 - Yes : 1");
		deconnexion = sc.nextInt();
		if (deconnexion == 1) {
			twitter.destroySession();
		}
	}

	public void addStatusPanel(Status s) {

		TweetPanel l = new TweetPanel(s.getUser().getScreenName(), s.getUser().getName(), s.getText(), s.getCreatedAt().toString(), s.getUser().getProfileImageURL().toString());
		l.setVisible(true);
		panelTweet.add(l);
		validate();
	}

	// setBackgroundImage instead ?

	public ImageIcon getBackgroundImage() {
		ImageIcon userBackground = new ImageIcon(twitter.getBackgroundImageURL());
		return userBackground;
	}

}
